package com.rc.saas.mini.hlhtcec2.printer;

import com.rc.saas.mini.hlhtcec2.printer.operator.HlhtPrinterXx;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 单例模式工厂
 * @author wangzhengwei
 * @create 2021/4/23
 */
public class HlhtPrinterStrategyFactory {
    private Map<String, HlhtPrintStrategy> map;

    public HlhtPrinterStrategyFactory() {
        List<HlhtPrintStrategy> strategies = new ArrayList<>();

        strategies.add(new HlhtPrinterXx());
        //strategies.add(new GoldStrategy());
        //strategies.add(new PlatinumStrategy());
        //strategies.add(new PlatinumStrategy());

        // 在构造方法的时候，初始化好需要的Strategy，并把 list 转化为 map
        map = strategies.stream().collect(Collectors.toMap(HlhtPrintStrategy::getHlhtOperatorId, strategy -> strategy));

        /* 等同上面
        map = new HashMap<>();
        for (Strategy strategy : strategies) {
            map.put(strategy.getType(), strategy);
        }*/
    }

    public static class Holder {
        public static HlhtPrinterStrategyFactory instance = new HlhtPrinterStrategyFactory();
    }

    public static HlhtPrinterStrategyFactory getInstance() {
        return HlhtPrinterStrategyFactory.Holder.instance;
    }

    public HlhtPrintStrategy get(String hlhtOperatorId) {
        return map.get(hlhtOperatorId);
    }
}
